<?php


function createThumbs( $pathToImages, $pathToThumbs, $thumbWidth ) {
   // open the directory
   $dir = opendir( $pathToImages );

   // loop through it, looking for any/all JPG files:
   while (false !== ($fname = readdir( $dir ))) {
      // parse path for the extension
      $info = pathinfo($pathToImages . $fname);
      // continue only if this is a JPEG image
      if ( strtolower($info['extension']) == 'jpg' ) {
         //echo "Creating thumbnail for {$fname} <br />";

         // load image and get image size
         $img = imagecreatefromjpeg( "{$pathToImages}{$fname}" );
         $width = imagesx( $img );
         $height = imagesy( $img );

         // calculate thumbnail size
         $new_width = $thumbWidth;
         $new_height = floor( $height * ( $thumbWidth / $width ) );

         // create a new temporary image
         $tmp_img = imagecreatetruecolor( $new_width, $new_height );

         // copy and resize old image into new image
         imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );

         // save thumbnail into a file
         imagejpeg( $tmp_img, "{$pathToThumbs}{$fname}" );
      }
   }
   // close the directory
   closedir( $dir );
}

function generateThumb( $imagePath, $outputDir, $thumbWidth ) {

   if( !file_exists($outputDir) && !mkdir($outputDir, 0777) ) {
      return;
   }

   /*if( fileperms($outputDir) != 0777 ) {
      @chmod($outputDir, 0777);
   }*/

   // get image file path information
   $info = pathinfo($imagePath);

   $img = NULL;
   // load image and get image size
   if ( strtolower($info['extension']) == 'jpg' ) {
      $img = imagecreatefromjpeg( $imagePath );
   } else if ( strtolower($info['extension']) == 'gif' ) {
      $img = imagecreatefromgif( $imagePath );
   } else if ( strtolower($info['extension']) == 'png' ) {
      $img = imagecreatefrompng( $imagePath );
   }

   $width = imagesx( $img );
   $height = imagesy( $img );

   // calculate thumbnail size
   $new_width = $thumbWidth;
   $new_height = floor( $height * ( $thumbWidth / $width ) );


   if( $new_width < $width || $new_height < $height) {
      // create a new temporary image
      $tmp_img = imagecreatetruecolor( $new_width, $new_height );
	  
	  if ( strtolower($info['extension']) == 'png' ) {
		  // work on image transparency
		  imagealphablending($tmp_img, false);
		  imagesavealpha($tmp_img, true);
	  }

      // copy and resize old image into new image
      imagecopyresampled($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );

      // save thumbnail into a file
      $fpath = "{$outputDir}{$info['basename']}";

      if ( strtolower($info['extension']) == 'jpg' ) {
         imagejpeg( $tmp_img, "{$outputDir}{$info['basename']}");
      } else if ( strtolower($info['extension']) == 'gif' ) {
         imagegif( $tmp_img, "{$outputDir}{$info['basename']}");
      } else if ( strtolower($info['extension']) == 'png' ) { 
		 imagepng( $tmp_img, "{$outputDir}{$info['basename']}");
      }

      return $fpath;
   } else {
      return $imagePath;
   }
}

?>
